package com.linser.gistools.refactoring.geotools;

import com.linser.gistools.refactoring.geotools.tools.CoordinateDisplayTool;
import com.linser.gistools.refactoring.geotools.tools.ScaleDisplayTool;
import javafx.scene.control.TextField;
import org.geotools.map.MapContent;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.swing.JMapPane;
import org.geotools.swing.tool.PanTool;
import org.geotools.swing.tool.ScrollWheelTool;

public class JMapPaneNew extends JMapPane{
    private final MapContent mapContent;

    private TextField coordinateText;

    private TextField scaleText;

    //是否开启地图允许默认操作（缩放、拖动）
    private boolean defaultMapOperate = true;
    public JMapPaneNew(MapContent content) {
        // 初始化地图内容...
        if(content != null){
            this.mapContent = content;
        }else {
            this.mapContent = new MapContent();
        }
        this.setMapContent(this.mapContent);

        // 创建渲染器
        GTRenderer renderer = new StreamingRenderer();
        this.setRenderer(renderer);
        //TODO 默认地图操作可配置
        this.defaultMapOperate = true;

        this.initDefaultEvent();
        // 添加鼠标监听器...
    }

    private void initDefaultEvent() {
        if(this.defaultMapOperate){
            //添加拖动工具
            PanTool panTool = new PanTool();
            panTool.setMapPane(this);
            addMouseListener(panTool);
            //添加滚轮缩放工具
            addMouseListener(new ScrollWheelTool(this));


            // 创建用于显示比例尺的 TextField
            this.scaleText = new TextField("");
            this.scaleText.setPrefHeight(25);
            this.scaleText.setMinHeight(25);
            this.scaleText.setMaxHeight(25);
            addMouseListener(new ScaleDisplayTool(this,this.scaleText));

            // 创建用于显示坐标的 JLabel
            this.coordinateText = new TextField("");
            this.coordinateText.setPrefHeight(25);
            this.coordinateText.setMinHeight(25);
            this.coordinateText.setMaxHeight(25);
            // 创建 CoordinateDisplayTool 对象并添加到 JMapPane 中
            CoordinateDisplayTool coordinateDisplayTool = new CoordinateDisplayTool(coordinateText);
            this.setCursorTool(coordinateDisplayTool);
            // 将标签添加到 JMapPane 的子组件中
            //this.add(coordinateLabel, BorderLayout.SOUTH);
        }
    }

    public TextField getCoordinateText() {
        return coordinateText;
    }

    public TextField getScaleText() {
        return scaleText;
    }
}
